#include "UsrAI.h"
#include<set>
#include <bits/stdc++.h>
#include<unordered_map>
using namespace std;
tagGame tagUsrGame;
ins UsrIns;
static int fram1=999999;
int f=1;//房屋参数
static int stage=0;
static int housebuildingstage=0;
tagFarmer yangmans[16],treemans[999999],fishboats[999999];
tagBuilding home[12];
tagBuilding home1[10];
static int housenum1=-1;
tagFarmer farmbuilder[999999];
int yangman=0,treeman=0,fishmannum=0,fishboat=0,farmbuildernum=0;
tagInfo info;
int begin_x = 0, begin_y = 0;
int center_x = 0, center_y = 0;
bool home_flag = false;
static int skillwood=0;
//计算距离
double distance(double x,double y,double a,double b){
    double ans=(abs(x)-abs(a))*(abs(x)-abs(a))+(abs(y)-abs(b))*(abs(y)-abs(b));
    ans=sqrt(ans);
    return ans;

}
//判断是否靠岸
bool whetherload(int&x, int&y, int width, int length){
    int i=x,j=y;
    if((*info.theMap)[i+2][j].type==MAPPATTERN_GRASS&&(*info.theMap)[i+2][j+1].type==MAPPATTERN_GRASS)
        return 1;
    else if((*info.theMap)[i-1][j].type==MAPPATTERN_GRASS&&(*info.theMap)[i-1][j+1].type==MAPPATTERN_GRASS)
        return 1;
    else if((*info.theMap)[i][j+2].type==MAPPATTERN_GRASS&&(*info.theMap)[i+1][j+2].type==MAPPATTERN_GRASS)
        return 1;
    else if((*info.theMap)[i][j-1].type==MAPPATTERN_GRASS&&(*info.theMap)[i+1][j-1].type==MAPPATTERN_GRASS)
        return 1;
    else return 0;
}
bool whetheroccupyed1(int&x1, int&y1, int width, int length,int width0,int length0){
    for(tagBuilding& occupy1 : info.buildings){
        int i1=occupy1.BlockDR,j1=occupy1.BlockUR;
                for(int i=x1;i<x1+width;i++){
                    for(int j=y1;j<y1+length;j++){
                        for(int i2=i1;i2<i1+width0;i2++){
                            for(int j2=j1;j2<j1+length0;j2++){
                                if(i==i2&&j==j2)
                                {

                                    return 0;
                                }
                            }
                        }
                    }
                }
    }
    for(tagFarmer& occupy2 : info.farmers){
        if(occupy2.BlockDR<x1+width&& occupy2.BlockDR>=x1
         &&occupy2.BlockUR<y1+length&&occupy2.BlockUR>=y1)
        {
            return 0;
        }
    }
    for(tagResource& occupy2 : info.resources){
        if(occupy2.BlockDR<x1+width&& occupy2.BlockDR>=x1
         &&occupy2.BlockUR<y1+length&&occupy2.BlockUR>=y1)
        {
            return 0;
        }
    }
    return 1;
}
bool whetheroccupyed2(int&x1, int&y1, int width, int length,int width0,int length0){
    for(tagBuilding& occupy1 : info.buildings){
        int i1=occupy1.BlockDR,j1=occupy1.BlockUR;
                for(int i=x1;i<x1+width;i++){
                    for(int j=y1;j<y1+length;j++){
                        for(int i2=i1;i2<i1+width0;i2++){
                            for(int j2=j1;j2<j1+length0;j2++){
                                if(i==i2&&j==j2)
                                {

                                    return 0;
                                }
                            }
                        }
                    }
                }
    }
}
bool whetheroccupyed(int&x1, int&y1, int width, int length,int width0,int length0){
    for(tagBuilding& occupy1 : info.buildings){
        int i1=occupy1.BlockDR,j1=occupy1.BlockUR;
                for(int i=x1;i<x1+width;i++){
                    for(int j=y1;j<y1+length;j++){
                        for(int i2=i1;i2<i1+width0;i2++){
                            for(int j2=j1;j2<j1+length0;j2++){
                                if(i==i2&&j==j2)
                                {
                                    x1=occupy1.BlockDR;
                                    y1=occupy1.BlockUR;
                                    return 0;
                                }
                            }
                        }
                    }
                }


    }
    for(tagFarmer& occupy : info.farmers){
        if(occupy.BlockDR<x1+width&& occupy.BlockDR>=x1
         &&occupy.BlockUR<y1+length&&occupy.BlockUR>=y1)
        {
            x1=occupy.BlockDR;
            y1=occupy.BlockUR;
            return 0;
        }
    }
    for(tagResource& occupy2 : info.resources){
        if(occupy2.BlockDR<x1+width&& occupy2.BlockDR>=x1
         &&occupy2.BlockUR<y1+length&&occupy2.BlockUR>=y1)
        {
            x1=occupy2.BlockDR;
            y1=occupy2.BlockUR;
            return 0;
        }
    }
    return 1;
}
// 从市镇中心开始查找2*2的空地，通过引用返回空地左下角坐标x,y
bool find_block(int& x, int& y,int width, int length, int landtype) {
    int i,j,sign0=1,sign=1;
    int m=x,n=y;
    int m1=x,n1=y;
    int num=0;
    double dismin=999999;
    //1
    for (i = x +  width; i < 128; i++) {
        for ( j = y+length ; j < 128; j++) {
            if (i + 1 < 128 && j + 1 < 128) {
                if(whetherload(i,j,width,length)==0) continue;
                for (int a = i; a< i+width; a++) {
                    for (int b= j;b < j+length; b++)
                       if((*info.theMap)[a][b].type != landtype)
                         sign0=0;
                }

         // 检查高度是否相等（平地）
               if(sign0==0) break;


                int height = (*info.theMap)[i][j].height;
                sign0=1;
                for (int a = i; a < i+width+1; a++) {
                    for (int b = j;b < j+length+1; b++)
                       if((*info.theMap)[a][b].height != height) {
                         sign0=0;
                       }
                    }
                if(sign0==0) break;
                if(sign0==1){
                    if(whetheroccupyed1(i,j,width,length,3,3)==0) {
                        m1=i,n1=j;
                        sign=0;
                        num++;
                        break;
                    }
                    double dis=distance(x ,y ,i , j);
                    if(dis<dismin)
                    {
                        dismin=dis;
                        m=i;
                        n=j;
                        break; //记录距离
                    }


                }
            }
        }
        if(sign==0) break;
        if(sign0==1) break;

    }
                //2
                sign=1;
                sign0=1;
                for (i = x ; i < 128; i++) {
                    for ( j = y -  length; j >0; j--) {
                        if (i + 1 < 128 && j - 1 > 0) {
                            if(whetherload(i,j,width,length)==0) continue;

                            // 检查四个位置是否都是陆地
                            for (int a = i; a< i+width; a++) {
                                for (int b= j;b > j-length; b--)
                                   if((*info.theMap)[a][b].type != landtype)
                                     sign0=0;
                            }

                          if(sign0==0) break;

                            int height = (*info.theMap)[i][j].height;
                            sign0=1;
                            for (int a = i; a < i+width+1; a++) {
                                for (int b = j;b > j-length-1; b--)
                                   if((*info.theMap)[a][b].height != height) {
                                     sign0=0;
                                   }
                                }
                            if(sign0==0) break;
                            if(sign0==1){
                                if(whetheroccupyed1(i,j,width,length,3,3)==0) {
                                    sign=0;
                                    num++;
                                    break;
                                }
                                double dis=distance(x ,y ,i , j);
                                if(dis<dismin)
                                {
                                    dismin=dis;
                                    m=i;
                                    n=j;
                                    break; //记录距离
                                }


                            }
                   }
              }
                    if(sign==0) break;
                    if(sign0==1) break;
         }
                //3
                sign=1;
                sign0=1;
                for (i = x -  width; i >0; i--) {
                    for ( j = y; j < 128; j++) {
                        if (i - 1>0 && j + 1 < 128) {
                            if(whetherload(i,j,width,length)==0) continue;

                            for (int a = i; a > i-width; a--) {
                                for (int b= j;b < j+length; b++)
                                   if((*info.theMap)[a][b].type != landtype)
                                     sign0=0;
                            }

                          if(sign0==0) break;

                            int height = (*info.theMap)[i][j].height;
                            sign0=1;
                            for (int a = i; a > i-width-1; a--) {
                                for (int b = j;b < j+length+1; b++)
                                   if((*info.theMap)[a][b].height != height) {
                                     sign0=0;
                                   }
                                }
                            if(sign0==0) break;
                            if(sign0==1){
                                if(whetheroccupyed1(i,j,width,length,3,3)==0) {
                                    sign=0;
                                    num++;
                                    break;
                                }
                                double dis=distance(x ,y ,i , j);
                                if(dis<dismin)
                                {
                                    dismin=dis;
                                    m=i;
                                    n=j;
                                    break; //记录距离
                                }


                            }
                        }
                    }
                    if(sign==0) break;
                    if(sign0==1) break;
                }
                            //4
                sign=1;
                sign0=1;
                            for (i = x- width ; i >0 ; i--) {
                                for ( j = y-  length ; j >0 ; j--) {
                                    if (i -1 >0 && j- 1 > 0) {
                                       if(whetherload(i,j,width,length)==0) continue;

                                        for (int a = i; a> i-width; a--) {
                                            for (int b= j;b > j-length; b--)
                                               if((*info.theMap)[a][b].type != landtype)
                                                 sign0=0;
                                        }

                                      if(sign0==0) break;

                                        int height = (*info.theMap)[i][j].height;
                                        sign0=1;
                                        for (int a = i; a > i-width-1; a--) {
                                            for (int b = j;b > j-length-1; b--)
                                               if((*info.theMap)[a][b].height != height) {
                                                 sign0=0;
                                               }
                                            }
                                        if(sign0==0) break;
                                        if(sign0==1){
                                            if(whetheroccupyed1(i,j,width,length,3,3)==0) {
                                                sign=0;
                                                num++;
                                                break;
                                            }
                                            double dis=distance(x ,y ,i , j);
                                            if(dis<dismin)
                                            {
                                                dismin=dis;
                                                m=i;
                                                n=j;
                                                break; //记录距离
                                            }
                                        }
                                    }
                                }
                                if(sign==0) break;
                                if(sign0==1) break;
                            }


                            if(num==4){
                                m=m1,n=n1;
                            }
                            x=m;
                            y=n;
                            return 1;
                     return 0; // 返回0表示未找到合适的空地
}
bool find_stock(int& x, int& y,int width, int length, int landtype) {
    int i,j,sign0=1,sign=1;
    int m=x,n=y;
    int m1=x,n1=y;
    int num=0;
    double dismin=999999;
    //1
    for (i = x +  width; i < 128; i++) {
        for ( j = y ; j < 128; j++) {
            if (i + 1 < 128 && j + 1 < 128) {
                if(whetherload(i,j,width,length)==0) continue;
                for (int a = i; a< i+width; a++) {
                    for (int b= j;b < j+length; b++)
                       if((*info.theMap)[a][b].type != landtype)
                         sign0=0;
                }

         // 检查高度是否相等（平地）
               if(sign0==0) break;


                int height = (*info.theMap)[i][j].height;
                sign0=1;
                for (int a = i; a < i+width+1; a++) {
                    for (int b = j;b < j+length+1; b++)
                       if((*info.theMap)[a][b].height != height) {
                         sign0=0;
                       }
                    }
                if(sign0==0) break;
                if(sign0==1){
                    if(whetheroccupyed1(i,j,width,length,3,3)==0) {
                        m1=i,n1=j;
                        sign=0;
                        num++;
                        break;
                    }
                    double dis=distance(x ,y ,i , j);
                    if(dis<dismin)
                    {
                        dismin=dis;
                        m=i;
                        n=j;
                        break; //记录距离
                    }


                }
            }
        }
        if(sign==0) break;
        if(sign0==1) break;

    }
                //2
                sign=1;
                sign0=1;
                for (i = x ; i < 128; i++) {
                    for ( j = y -  length; j >0; j--) {
                        if (i + 1 < 128 && j - 1 > 0) {
                            if(whetherload(i,j,width,length)==0) continue;

                            // 检查四个位置是否都是陆地
                            for (int a = i; a< i+width; a++) {
                                for (int b= j;b > j-length; b--)
                                   if((*info.theMap)[a][b].type != landtype)
                                     sign0=0;
                            }

                          if(sign0==0) break;

                            int height = (*info.theMap)[i][j].height;
                            sign0=1;
                            for (int a = i; a < i+width+1; a++) {
                                for (int b = j;b > j-length-1; b--)
                                   if((*info.theMap)[a][b].height != height) {
                                     sign0=0;
                                   }
                                }
                            if(sign0==0) break;
                            if(sign0==1){
                                if(whetheroccupyed1(i,j,width,length,3,3)==0) {
                                    sign=0;
                                    num++;
                                    break;
                                }
                                double dis=distance(x ,y ,i , j);
                                if(dis<dismin)
                                {
                                    dismin=dis;
                                    m=i;
                                    n=j;
                                    break; //记录距离
                                }


                            }
                   }
              }
                    if(sign==0) break;
                    if(sign0==1) break;
         }
                //3
                sign=1;
                sign0=1;
                for (i = x -  width; i >0; i--) {
                    for ( j = y; j < 128; j++) {
                        if (i - 1>0 && j + 1 < 128) {
                            if(whetherload(i,j,width,length)==0) continue;

                            for (int a = i; a > i-width; a--) {
                                for (int b= j;b < j+length; b++)
                                   if((*info.theMap)[a][b].type != landtype)
                                     sign0=0;
                            }

                          if(sign0==0) break;

                            int height = (*info.theMap)[i][j].height;
                            sign0=1;
                            for (int a = i; a > i-width-1; a--) {
                                for (int b = j;b < j+length+1; b++)
                                   if((*info.theMap)[a][b].height != height) {
                                     sign0=0;
                                   }
                                }
                            if(sign0==0) break;
                            if(sign0==1){
                                if(whetheroccupyed1(i,j,width,length,3,3)==0) {
                                    sign=0;
                                    num++;
                                    break;
                                }
                                double dis=distance(x ,y ,i , j);
                                if(dis<dismin)
                                {
                                    dismin=dis;
                                    m=i;
                                    n=j;
                                    break; //记录距离
                                }


                            }
                        }
                    }
                    if(sign==0) break;
                    if(sign0==1) break;
                }
                            //4

                sign0=1;
                            for (i = x- width ; i >0 ; i--) {
                                for ( j = y-  length ; j >0 ; j--) {
                                    if (i -1 >0 && j- 1 > 0) {
                                       if(whetherload(i,j,width,length)==0) continue;

                                        for (int a = i; a> i-width; a--) {
                                            for (int b= j;b > j-length; b--)
                                               if((*info.theMap)[a][b].type != landtype)
                                                 sign0=0;
                                        }

                                      if(sign0==0) break;

                                        int height = (*info.theMap)[i][j].height;
                                        sign0=1;
                                        for (int a = i; a > i-width-1; a--) {
                                            for (int b = j;b > j-length-1; b--)
                                               if((*info.theMap)[a][b].height != height) {
                                                 sign0=0;
                                               }
                                            }
                                        if(sign0==0) break;
                                        if(sign0==1){
                                            if(whetheroccupyed1(i,j,width,length,3,3)==0) {
                                                sign=0;
                                                num++;
                                                break;
                                            }
                                            double dis=distance(x ,y ,i , j);
                                            if(dis<dismin)
                                            {
                                                dismin=dis;
                                                m=i;
                                                n=j;
                                                break; //记录距离
                                            }
                                        }
                                    }
                                }
                                if(sign==0) break;
                                if(sign0==1) break;
                            }


                            if(num==4){
                                m=m1,n=n1;
                            }
                            x=m;
                            y=n;
                            return 1;
                     return 0; // 返回0表示未找到合适的空地
}
bool find_shore(int& x, int& y,int width, int length, int landtype) {
    int i,j,sign0=1;
    int a=x,b=x;
    double dismin=999999;
    //1
    for (i = x +  width; i < 128; i++) {
        for ( j = y +  length; j < 128; j++) {
            if (i + 1 < 128 && j + 1 < 128) {
                if(whetherload(i,j,width,length)==0) continue;
                for (int a = i; a< i+width; a++) {
                    for (int b= j;b < j+length; b++)
                       if((*info.theMap)[a][b].type != landtype)
                         sign0=0;
                }

         // 检查高度是否相等（平地）
               if(sign0==0) continue;



                int height = (*info.theMap)[i][j].height;
                sign0=1;
                for (int a = i; a < i+width; a++) {
                    for (int b = j;b < j+length; b++)
                       if((*info.theMap)[a][b].height != height) {
                         sign0=0;
                       }
                    }
                if(sign0==0) continue;
                if(sign0==1){
                    double dis=distance(x ,y ,i , j);
                    if(dis<dismin)
                    {
                        dismin=dis;
                        a=i;
                        b=j;
                    }
                    break; //记录距离
                }
            }
        }
        if(sign0==1)  break;
    }
                //2
                sign0=1;
                for (i = x ; i < 128; i++) {
                    for ( j = y -  length; j >0; j--) {
                        if (i + 1 < 128 && j - 1 > 0) {
                            if(whetherload(i,j,width,length)==0) continue;
                            // 检查四个位置是否都是陆地
                            for (int a = i; a< i+width; a++) {
                                for (int b= j;b > j-length; b--)
                                   if((*info.theMap)[a][b].type != landtype)
                                     sign0=0;
                            }

                          if(sign0==0) continue;

                            int height = (*info.theMap)[i][j].height;
                            sign0=1;
                            for (int a = i; a < i+width; a++) {
                                for (int b = j;b > j-length; b--)
                                   if((*info.theMap)[a][b].height != height) {
                                     sign0=0;
                                   }
                                }
                            if(sign0==0) continue;
                    if(sign0==1){
                        double dis=distance(x ,y ,i , j);
                        if(dis<dismin)
                        {
                            dismin=dis;
                            a=i;
                            b=j;
                        }
                        break; //记录距离
                    }
                   }
              }
                    if(sign0==1) break;
         }
                //3
                sign0=1;
                for (i = x -  width; i >0; i--) {
                    for ( j = y; j < 128; j++) {
                        if (i - 1>0 && j + 1 < 128) {
                            if(whetherload(i,j,width,length)==0) continue;
                            for (int a = i; a > i-width; a--) {
                                for (int b= j;b < j+length; b++)
                                   if((*info.theMap)[a][b].type != landtype)
                                     sign0=0;
                            }

                          if(sign0==0) continue;

                            int height = (*info.theMap)[i][j].height;
                            sign0=1;
                            for (int a = i; a > i-width; a--) {
                                for (int b = j;b < j+length; b++)
                                   if((*info.theMap)[a][b].height != height) {
                                     sign0=0;
                                   }
                                }
                            if(sign0==0) continue;
                            if(sign0==1){
                                double dis=distance(x ,y ,i , j);
                                if(dis<dismin)
                                {
                                    dismin=dis;
                                    a=i;
                                    b=j;
                                }
                                break; //记录距离
                            }
                        }
                    }
                    if(sign0==1) break;
                }
                            //4
                            sign0=1;
                            for (i = x- width ; i >0 ; i--) {
                                for ( j = y-  length ; j >0 ; j--) {
                                    if (i -1 >0 && j- 1 > 0) {
                                       if(whetherload(i,j,width,length)==0) continue;
                                        for (int a = i; a> i-width; a--) {
                                            for (int b= j;b > j-length; b--)
                                               if((*info.theMap)[a][b].type != landtype)
                                                 sign0=0;
                                        }

                                      if(sign0==0) continue;

                                        int height = (*info.theMap)[i][j].height;
                                        sign0=1;
                                        for (int a = i; a > i-width; a--) {
                                            for (int b = j;b > j-length; b--)
                                               if((*info.theMap)[a][b].height != height) {
                                                 sign0=0;
                                               }
                                            }
                                        if(sign0==0) continue;
                                        if(sign0==1){
                                            double dis=distance(x ,y ,i , j);
                                            if(dis<dismin)
                                            {
                                                dismin=dis;
                                                a=i;
                                                b=j;
                                            }
                                            break; //记录距离
                                        }
                                    }
                                }
                                if(sign0==1) break;
                            }
                            x=a;
                            y=b;
                            return 1;
                     return 0; // 返回0表示未找到合适的空地
}
bool find_home(int& x, int& y,int width, int length, int landtype) {
    int i,j,sign0=1;
    int a=x,b=x;


    //1
    for (i = x+width; i < 128; i++) {
        for ( j = y ; j < 128; j++) {
            if (i + 1 < 128 && j + 1 < 128) {
                if(whetherload(i,j,width,length)==0) continue;

                for (int a = i; a< i+width; a++) {
                    for (int b= j;b < j+length; b++)
                       if((*info.theMap)[a][b].type != landtype)
                         sign0=0;
                }

         // 检查高度是否相等（平地）
               if(sign0==0) continue;
                int height = (*info.theMap)[i][j].height;
                sign0=1;
                for (int a = i; a <= i+width; a++) {
                    for (int b = j;b <= j+length; b++)
                       if((*info.theMap)[a][b].height != height) {
                         sign0=0;
                       }
                }
                if(sign0==0) continue;
                x=i;
                y=j;
                return 1;
            }
        }
        if(sign0==1)  break;
    }
    //5
    for (i = x; i < 128; i++) {
        for ( j = y+length ; j < 128; j++) {
            if (i + 1 < 128 && j + 1 < 128) {
                if(whetherload(i,j,width,length)==0) continue;

                for (int a = i; a< i+width; a++) {
                    for (int b= j;b < j+length; b++)
                       if((*info.theMap)[a][b].type != landtype)
                         sign0=0;
                }

         // 检查高度是否相等（平地）
               if(sign0==0) continue;
                int height = (*info.theMap)[i][j].height;
                sign0=1;
                for (int a = i; a <= i+width; a++) {
                    for (int b = j;b <= j+length; b++)
                       if((*info.theMap)[a][b].height != height) {
                         sign0=0;
                       }
                }
                if(sign0==0) continue;
                x=i;
                y=j;
                return 1;
            }
        }
        if(sign0==1)  break;
    }
                //2
                sign0=1;
                for (i = x ; i < 128; i++) {
                    for ( j = y -  length; j >0; j--) {
                        if (i + 1 < 128 && j - 1 > 0) {
                            if(whetherload(i,j,width,length)==0) continue;
                            // 检查四个位置是否都是陆地
                            for (int a = i; a< i+width; a++) {
                                for (int b= j;b > j-length; b--)
                                   if((*info.theMap)[a][b].type != landtype)
                                     sign0=0;
                            }

                          if(sign0==0) continue;

                            int height = (*info.theMap)[i][j].height;
                            sign0=1;
                            for (int a = i; a <=i+width+1; a++) {
                                for (int b = j;b >=j-length-1; b--)
                                   if((*info.theMap)[a][b].height != height) {
                                     sign0=0;
                                   }
                                }
                            if(sign0==0) continue;
                            x=i;
                            y=j;
                            return 1;
                   }
              }
                    if(sign0==1) break;
         }
                //3
                sign0=1;
                for (i = x -  width; i >0; i--) {
                    for ( j = y; j < 128; j++) {
                        if (i - 1>0 && j + 1 < 128) {
                            if(whetherload(i,j,width,length)==0) continue;
                            for (int a = i; a > i-width; a--) {
                                for (int b= j;b < j+length; b++)
                                   if((*info.theMap)[a][b].type != landtype)
                                     sign0=0;
                            }

                          if(sign0==0) continue;

                            int height = (*info.theMap)[i][j].height;
                            sign0=1;
                            for (int a = i; a >= i-width; a--) {
                                for (int b = j;b <= j+length; b++)
                                   if((*info.theMap)[a][b].height != height) {
                                     sign0=0;
                                   }
                                }
                            if(sign0==0) continue;
                            x=i;
                            y=j;
                            return 1;
                        }
                    }
                    if(sign0==1) break;
                }
                            //4
                            sign0=1;
                            for (i = x- width ; i >0 ; i--) {
                                for ( j = y-  length ; j >0 ; j--) {
                                    if (i -1 >0 && j- 1 > 0) {
                                       if(whetherload(i,j,width,length)==0) continue;
                                        for (int a = i; a> i-width; a--) {
                                            for (int b= j;b > j-length; b--)
                                               if((*info.theMap)[a][b].type != landtype)
                                                 sign0=0;
                                        }

                                      if(sign0==0) continue;

                                        int height = (*info.theMap)[i][j].height;
                                        sign0=1;
                                        for (int a = i; a >= i-width; a--) {
                                            for (int b = j;b >= j-length; b--)
                                               if((*info.theMap)[a][b].height != height) {
                                                 sign0=0;
                                               }
                                            }
                                        if(sign0==0) continue;
                                        x=i;
                                        y=j;
                                        return 1;
                                    }
                                }
                                if(sign0==1) break;
                            }
                     return 0; // 返回0表示未找到合适的空地
}
//找最远距离
tagResource roadmax(int& x, int& y, int type){
    int dismax=0;
    tagResource source;
    for (tagResource& resource0 : info.resources){
        if(resource0.Type==type){
            int dis=distance(resource0.BlockDR,resource0.BlockUR, x, y);
            if(dis>dismax){
               dismax=dis;
               source=resource0;
            }
        }
    }
    return source;
}
//找最近距离
tagResource roadmin(int& x, int& y, int type){
    int dismin=999999;
    tagResource source;
    for (tagResource& resource0 : info.resources){
        if(resource0.Type==type){
            int dis=distance(resource0.BlockDR,resource0.BlockUR, x, y);
            if(dis<dismin){
               dismin=dis;
               source=resource0;
            }
        }
    }
    return source;
}
void UsrAI::processData()
{
    info = getInfo();
    tagBuilding center;
       // 初始化市镇中心坐标
       if (center_x == 0 && center_y == 0) {
           for (tagBuilding& building : info.buildings) {
               if (building.Type == BUILDING_CENTER) {
                   center=building;
                   center_x = building.BlockDR;
                   center_y = building.BlockUR;
               }
           }
       }
    cout<<"stage:"<<stage<<" "<<"skillwood:"<<skillwood<<endl;
    switch (stage) {
        case 0:{
       //造人
       info = getInfo();
       int mannum=0;
       for(tagFarmer& man : info.farmers){
           if(man.FarmerSort==0)
               mannum++;
       }
           for (tagBuilding& building : info.buildings) {
               //如果市镇中心空闲，且有足够的食物，且农民数量小于人口上限，则生产农民
               if (building.Type == BUILDING_CENTER && building.Project == 0 && info.Meat >= BUILDING_CENTER_CREATEFARMER_FOOD &&
                   mannum < 12) {
                   BuildingAction(building.SN, BUILDING_CENTER_CREATEFARMER);
                   break;
               }
           }
           //造屋
                   info = getInfo();
                    int home_x=center_x, home_y=center_y;
                    if(housebuildingstage==0)
                     while (find_home(home_x, home_y,  4, 8,MAPPATTERN_GRASS)){
                          if(whetheroccupyed(home_x,home_y,4,8,1,1)==0) continue;
                          for(int i=0;i<4;i++)
                          {
                              home[i].BlockDR=home_x;
                              home[i].BlockUR=home_y+i*2;
                          }
                          housebuildingstage++;
                          break;
                      }

                      //小屋数量
                      int housenum=0;
                      for(tagBuilding& house : info.buildings){
                          if(house.Type==BUILDING_HOME)
                              housenum++;
                      }
                          int farmersn=999999;
                          f=0;
                          tagFarmer housebuilder;
                          for (tagFarmer& farmer : info.farmers) {
                                 if(farmer.SN<farmersn){
                                     farmersn=farmer.SN;
                                     housebuilder=farmer;  //固定建房人

                                 }
                                 f++;
                               if(f==info.farmers.size())
                               {
                                 if(housenum<4){
                                     if (housebuilder.NowState == HUMAN_STATE_IDLE) {
                                         if(whetheroccupyed1(home[housenum].BlockDR, home[housenum].BlockUR,2,2,1,1)==0)
                                         {
                                            HumanMove(housebuilder.SN,home[housenum-1].BlockDR,home[housenum-1].BlockUR);
                                            return;
                                         }
                                         HumanBuild(housebuilder.SN, BUILDING_HOME, home[housenum].BlockDR, home[housenum].BlockUR);
                                     return;
                                     }
                                  }
                               }


                          }

                          //船坞
                          info=getInfo();
                          int docknum=0;//仓库参数
                              for (tagBuilding& building : info.buildings) {
                                      if (building.Type == BUILDING_DOCK) {
                                          docknum++;
                                      }
                              }
                          if(info.farmers.size()>5&&docknum==0&&info.Wood>=BUILD_DOCK_WOOD){
                              tagFarmer housebuilder2;
                              for(tagFarmer& farmer : info.farmers){
                                if(farmer.NowState==HUMAN_STATE_IDLE&&farmer.FarmerSort==0){
                                  housebuilder2=farmer;
                                  double dismin=9999999;
                                  tagResource fish;
                                  int x1=0,y1=0;
                                  for (tagResource& findfish : info.resources){
                                      if(findfish.Type==RESOURCE_FISH){
                                         x1=findfish.BlockDR,y1=findfish.BlockUR;
                                         if( whetherload(x1,y1,2,2)!=0){
                                             double dis=distance(x1,y1,center_x,center_y);
                                             if(dis<dismin){
                                                 dismin=dis;
                                                 fish=findfish;
                                             }
                                          }
                                       }
                                  }
                                  if(find_shore(fish.BlockDR,fish.BlockUR,2  ,2 ,MAPPATTERN_OCEAN)){
                                      HumanBuild(housebuilder2.SN,BUILDING_DOCK,fish.BlockDR,fish.BlockUR);
                                              for (tagBuilding& building : info.buildings) {
                                                      if (building.Type == BUILDING_DOCK) {
                                                          docknum++;
                                                      }
                                                  }
                                              if(docknum==1)
                                                  return;
                                  }

                              }
                            }
                          }
                          //船坞2
                                                  info=getInfo();
                                                  tagBuilding firstdock;
                                                  docknum=0;
                                                  for(tagBuilding&building : info.buildings){
                                                      if(building.Type==BUILDING_DOCK){
                                                          docknum++;
                                                          firstdock=building;
                                                      }

                                                  }
                                                  if(docknum==1&&info.Wood>=BUILD_DOCK_WOOD){
                                                      tagFarmer housebuilder2;
                                                      for(tagFarmer& farmer : info.farmers){
                                                        if(farmer.NowState==HUMAN_STATE_IDLE&&farmer.FarmerSort==0){
                                                          housebuilder2=farmer;
                                                          if(find_shore(firstdock.BlockDR,firstdock.BlockUR,2  ,2 ,MAPPATTERN_OCEAN)){

                                                              HumanBuild(housebuilder2.SN,BUILDING_DOCK,firstdock.BlockDR,firstdock.BlockUR);
                                                                      for (tagBuilding& building : info.buildings) {
                                                                              if (building.Type == BUILDING_DOCK) {
                                                                                  docknum++;
                                                                              }
                                                                          }
                                                                      if(docknum==2)
                                                                          return;
                                                          }

                                                      }
                                                    }
                                                  }
                          //渔船捕鱼
                          info= getInfo();
                          int checkfish=0;
                         for (tagResource& resource : info.resources){
                             if(resource.Type==RESOURCE_FISH)
                             {
                                 checkfish=1;
                                 break;
                             }
                         }
                         if(checkfish)
                          for(tagFarmer&boat : info.farmers){
                              if(boat.FarmerSort==2 && boat.NowState == HUMAN_STATE_IDLE){
                                  int dismin=999999;
                                  tagResource source;
                                  for (tagResource& resource : info.resources) {
                                     if (resource.Type == RESOURCE_FISH) {
                                         fishboats[fishboat]=boat;
                                         double dis=distance(resource.DR,resource.UR,boat.DR,boat.UR);
                                         if(dis<dismin)
                                         {
                                            dismin=dis;
                                            source=resource;
                                         }
                                      }
                                  }
                                  HumanAction(fishboats[fishboat].SN, source.SN);
                                  fishboat++;
                                  return;
                              }
                          }
                         int stocknum=0;//仓库参数
                             for (tagBuilding& building : info.buildings) {
                                     if (building.Type == BUILDING_STOCK) {
                                         stocknum++;
                                     }
                             }


                          //仓库

                          if(stocknum==1&&info.farmers.size()>5&&info.Wood>=BUILD_STOCK_WOOD){
                              tagFarmer housebuilder1;
                       for (tagFarmer& farmer : info.farmers) {
                          if (farmer.FarmerSort == 0 && farmer.NowState == HUMAN_STATE_IDLE) {
                              housebuilder1=farmer;
                              double dismax=0;
                              tagResource source;
                              for (tagResource& resource0 : info.resources){
                                  if(resource0.Type==RESOURCE_GAZELLE){
                                      double dis=distance(resource0.BlockDR,resource0.BlockUR,center_x,center_y);
                                      if(dis>dismax){
                                         dismax=dis;
                                         source=resource0;
                                      }
                                  }
                              }

                              while(find_stock(source.BlockDR,source.BlockUR,  3, 3, MAPPATTERN_GRASS)){
                                  if(whetheroccupyed(source.BlockDR, source.BlockUR, 3, 3,2,2)==0)
                                     continue;
                                  HumanBuild(housebuilder1.SN,BUILDING_STOCK,source.BlockDR,source.BlockUR);
                                  for (tagBuilding& building : info.buildings) {
                                          if (building.Type == BUILDING_STOCK) {
                                              stocknum++;
                                          }
                                      }
                                  if(stocknum==2)
                                      return;
                               }
                          }
                          return;
                       }
                          }

                          //造渔船
                          info = getInfo();
                          int boatnum=0;
                          for(tagFarmer& boat : info.farmers){
                              if(boat.FarmerSort==2)
                                  boatnum++;
                          }
                          if(stocknum==2&&boatnum<3){
                              for (tagBuilding& building : info.buildings) {
                                  if (building.Type == BUILDING_DOCK && building.Project == 0 && info.Wood >=BUILDING_DOCK_CREATE_SAILING_WOOD &&
                                      info.farmers.size() < info.Human_MaxNum) {
                                      BuildingAction(building.SN, BUILDING_DOCK_CREATE_SAILING);
                                      break;
                                  }
                              }
                          }

   //砍羊
       info = getInfo();
       int boolyang=1;
       for (tagResource& resource0 : info.resources){
           if(resource0.Type==RESOURCE_GAZELLE)
               boolyang=0;
       }

   if(boolyang==0){
   if(info.farmers.size()>5&&treeman>7&&stocknum==2){
     for (tagFarmer& farmer : info.farmers) {
        if (farmer.FarmerSort == 0 && farmer.NowState == HUMAN_STATE_IDLE) {
           yangmans[yangman]=farmer;
           int dismax=999999;
           tagResource source;
           for (tagResource& resource : info.resources) {
              if (resource.Type == RESOURCE_GAZELLE) {
                  int dis=distance(resource.DR,resource.UR,yangmans[yangman].DR,yangmans[yangman].UR);
                  if(dis<dismax)
                  {
                     dismax=dis;
                     source=resource;
                  }
               }
           }
           HumanAction(yangmans[yangman].SN, source.SN);
           yangman++;
           return;

        }
     }
   }
   }

     //砍树
     info = getInfo();
     for (tagFarmer& farmer : info.farmers) {
       if (farmer.FarmerSort == 0 && farmer.NowState == HUMAN_STATE_IDLE) {
          treemans[treeman]=farmer;
          int dismin=9999999;
          tagResource source;
          for (tagResource& resource : info.resources) {
             if (resource.Type == RESOURCE_TREE) {
                int dis=distance(resource.DR,resource.UR,treemans[treeman].DR,treemans[treeman].UR);
                if(dis<dismin)
                {
                    dismin=dis;
                    source=resource;
                }
              }
           }
           HumanAction(treemans[treeman].SN, source.SN);
           treeman++;
           return;
         }
      }

     //升时代
     for(tagBuilding& building : info.buildings){
         if(building.Type==BUILDING_CENTER && info.Meat>=500){
             BuildingAction(building.SN,BUILDING_CENTER_UPGRADE);
          //   if(building.ProjectPercent==1){
             if(boolyang==0)
             stage++;
          //   }
             return;
         }
     }

    }
        break;
        case 1:{
        //造人
        info = getInfo();
        int mannum=0;
        for(tagFarmer& man : info.farmers){
            if(man.FarmerSort==0)
                mannum++;
        }
        for (tagBuilding& building : info.buildings) {
        //如果市镇中心空闲，且有足够的食物，且农民数量小于人口上限，则生产农民
          if (building.Type == BUILDING_CENTER && building.Project == 0 && info.Meat >= BUILDING_CENTER_CREATEFARMER_FOOD &&
             mannum < 20) {
             BuildingAction(building.SN, BUILDING_CENTER_CREATEFARMER);
             break;
          }
        }
        int granarynum=0;
        int home_x=center_x,home_y=center_y;
        //2阶段建房
        info = getInfo();
               if(housebuildingstage==1){
                     //      while (find_home(home_x, home_y,  2, 8,MAPPATTERN_GRASS, 6)){
                               for(int i=4;i<8;i++)
                               {
                                   home[i].BlockDR=home[i-4].BlockDR+2;
                                   home[i].BlockUR=home[i-4].BlockUR;
                               }
                               housebuildingstage++;

                       //    }


                }

        //小屋数量
        int housenum=0;
        home_x=center_x,home_y=center_y;
        for(tagBuilding& house : info.buildings){
            if(house.Type==BUILDING_HOME)
                housenum++;
        }
            int farmersn=999999;
            f=0;
            tagFarmer housebuilder;
            if(info.Wood>=BUILD_HOUSE_WOOD)
            for (tagFarmer& farmer : info.farmers) {
                   if(farmer.SN<farmersn){
                       farmersn=farmer.SN;
                       housebuilder=farmer;  //固定建房人

                   }
                   f++;
                 if(f==info.farmers.size())
                   if(housenum<8){
                       if (housebuilder.NowState == HUMAN_STATE_IDLE) {
                           if(whetheroccupyed1(home[housenum].BlockDR, home[housenum].BlockUR,2,2,1,1)==0)
                           {
                              HumanMove(housebuilder.SN,home[housenum-1].BlockDR,home[housenum-1].BlockUR);
                              return;
                           }
                           HumanBuild(housebuilder.SN, BUILDING_HOME, home[housenum].BlockDR, home[housenum].BlockUR);
                           housenum++;
                       return;
                       }
                   }
            }
            //建谷仓
            info =getInfo();
            home_x=center_x,home_y=center_y;
            for(tagBuilding& granary :info.buildings){
                if(granary.Type==BUILDING_GRANARY){
                    granarynum++;
                }
            }
            if(granarynum==0&&info.Wood>=BUILD_GRANARY_WOOD){
                tagFarmer granarybuilder;
                for(tagFarmer& farmer :info.farmers){
                    if(farmer.FarmerSort==0&&farmer.NowState==HUMAN_STATE_IDLE){
                        granarybuilder=farmer;
                        while(find_block(home_x,home_y,3,3,MAPPATTERN_GRASS)){
                            if(whetheroccupyed1(home_x,home_y,3,3,3,3)==0) continue;
                            HumanBuild(granarybuilder.SN,BUILDING_GRANARY,home_x,home_y);
                            granarynum++;
                            return;
                        }
                    }
                    return;
                }
            }


        //建市场

        info =getInfo();
        tagFarmer marketbuilder;
        home_x=center_x,home_y=center_y;
        int marketnum=0;
        for(tagBuilding& market :info.buildings){
            if(market.Type==BUILDING_MARKET){
                marketnum++;
            }
        }
        if(marketnum==0&&granarynum==1&&info.Wood>=BUILD_MARKET_WOOD){

            for(tagFarmer& farmer :info.farmers){
                if(farmer.FarmerSort==0&&farmer.NowState==HUMAN_STATE_IDLE){
                    marketbuilder=farmer;
                    while(find_block(home_x,home_y,3,3,MAPPATTERN_GRASS)){
                        if(whetheroccupyed1(home_x,home_y,3,3,3,3)==0) continue;
                        HumanBuild(marketbuilder.SN,BUILDING_MARKET,home_x,home_y);
                        marketnum++;
                        return;
                    }
                }
                return;
            }
        }
        if(marketnum==1&&housenum==8)
            stage++;


                     //砍树
                     info = getInfo();
                     for (tagFarmer& farmer : info.farmers) {
                       if (farmer.FarmerSort == 0 && farmer.NowState == HUMAN_STATE_IDLE) {
                          treemans[treeman]=farmer;
                          int dismin=9999999;
                          tagResource source;
                          for (tagResource& resource : info.resources) {
                             if (resource.Type == RESOURCE_TREE) {
                                int dis=distance(resource.DR,resource.UR,treemans[treeman].DR,treemans[treeman].UR);
                                if(dis<dismin)
                                {
                                    dismin=dis;
                                    source=resource;
                                }
                              }
                           }
                           HumanAction(treemans[treeman].SN, source.SN);
                           treeman++;
                           return;
                         }
                      }

                     //造渔船
                     info = getInfo();
                     int boatnum=0;
                     for(tagFarmer& boat : info.farmers){
                         if(boat.FarmerSort==2)
                             boatnum++;
                     }
                     if(boatnum<5){
                         for (tagBuilding& building : info.buildings) {
                             if (building.Type == BUILDING_DOCK && building.Project == 0 && info.Wood >BUILDING_DOCK_CREATE_SAILING_WOOD &&
                                 info.farmers.size() < info.Human_MaxNum) {
                                 BuildingAction(building.SN, BUILDING_DOCK_CREATE_SAILING);
                                 break;
                             }
                         }
                     }
                     //渔船捕鱼
                    info= getInfo();
                    int checkfish=0;
                    for (tagResource& resource : info.resources){
                        if(resource.Type==RESOURCE_FISH)
                        {
                            checkfish=1;
                            break;
                        }
                    }
                    if(checkfish)
                     for(tagFarmer&boat : info.farmers){
                         if(boat.FarmerSort==2 && boat.NowState == HUMAN_STATE_IDLE){
                             int dismin=999999;
                             tagResource source;
                             for (tagResource& resource : info.resources) {
                                if (resource.Type == RESOURCE_FISH) {
                                    fishboats[fishboat]=boat;
                                    double dis=distance(resource.DR,resource.UR,boat.DR,boat.UR);
                                    if(dis<dismin)
                                    {
                                       dismin=dis;
                                       source=resource;
                                    }
                                 }
                             }
                             HumanAction(fishboats[fishboat].SN, source.SN);
                             fishboat++;
                             return;
                         }
                     }

    }
        break;
        case 2:{

        //升级伐木
        info=getInfo();
        if(skillwood==0)
        for(tagBuilding& building : info.buildings){
            if(building.Type==BUILDING_MARKET&&info.Wood>=125 &&info.Meat>= 75 && building.Project == 0 ){
                if(building.Blood==350)
                {
                    BuildingAction(building.SN,BUILDING_MARKET_WOOD_UPGRADE );
                    if(building.ProjectPercent>0.5)
                      skillwood++;
                    return;
                }
            }
        }
        //建农场
               info =getInfo();
               int farmnum=0;
               int home_x=center_x,home_y=center_y;

                int marketnum=0;

                 for(tagBuilding& building :info.buildings){
                     if(building.Type==BUILDING_GRANARY){
                         home_x=building.BlockDR,home_y=building.BlockUR;

                     }
                 }
                 for(tagBuilding& building :info.buildings){
                     if(building.Type==BUILDING_MARKET){
                             marketnum++;
                     }
                 }
                 for(tagBuilding& building :info.buildings){
                     if(building.Type==BUILDING_FARM){
                            home_x=building.BlockDR,home_y=building.BlockUR;
                           farmnum++;
                     }
                 }

                 int farmnum1=farmnum;
               if(farmnum==1)
                 for(tagBuilding& building :info.buildings){
                     if(building.Type==BUILDING_MARKET){
                         home_x=building.BlockDR,home_y=building.BlockUR;
                     }
                 }
                 cout<<"farmnum:"<<farmnum<<endl;
                if(marketnum==1&&farmnum<4&&info.Wood>=BUILD_FARM_WOOD){
                    for(tagFarmer& farmer :info.farmers){
                        if(farmer.FarmerSort==0&&farmer.NowState==HUMAN_STATE_IDLE){
                            farmbuilder[farmnum]=farmer;
                            while(find_block(home_x,home_y,3,3,MAPPATTERN_GRASS)){
                                if(whetheroccupyed1 (home_x,home_y,3,3,3,3)==0) break;
                                HumanBuild(farmbuilder[farmnum].SN,BUILDING_FARM,home_x,home_y);
                                farmnum++;
                                return;
                            }
                        }
                        if(farmnum1!=farmnum) return;

                    }
                }

                //造战船
                info = getInfo();
                int shipnum=0;
                for(tagArmy& boat : info.armies){
                    if(boat.Sort==AT_SHIP)
                        shipnum++;
                }
                if(shipnum<10){
                    for (tagBuilding& building : info.buildings) {
                        if (building.Type == BUILDING_DOCK && building.Project == 0 && info.Wood >BUILDING_DOCK_CREATE_SHIP_WOOD &&
                            info.farmers.size() < info.Human_MaxNum) {
                            BuildingAction(building.SN, BUILDING_DOCK_CREATE_SHIP);
                            break;
                        }
                    }
                }

                //砍树
                info = getInfo();
                for (tagFarmer& farmer : info.farmers) {
                  if (farmer.FarmerSort == 0 && farmer.NowState == HUMAN_STATE_IDLE) {
                     treemans[treeman]=farmer;
                     int dismin=9999999;
                     tagResource source;
                     for (tagResource& resource : info.resources) {
                        if (resource.Type == RESOURCE_TREE) {
                           int dis=distance(resource.DR,resource.UR,treemans[treeman].DR,treemans[treeman].UR);
                           if(dis<dismin)
                           {
                               dismin=dis;
                               source=resource;
                           }
                         }
                      }
                      HumanAction(treemans[treeman].SN, source.SN);
                      treeman++;
                      return;
                    }
                 }

                //渔船捕鱼
                info= getInfo();
                int checkfish=0;
               for (tagResource& resource : info.resources){
                   if(resource.Type==RESOURCE_FISH)
                   {
                       checkfish=1;
                       break;
                   }
               }
               if(checkfish)
                for(tagFarmer&boat : info.farmers){
                    if(boat.FarmerSort==2 && boat.NowState == HUMAN_STATE_IDLE){
                        int dismin=999999;
                        tagResource source;
                        for (tagResource& resource : info.resources) {
                           if (resource.Type == RESOURCE_FISH) {
                               fishboats[fishboat]=boat;
                               double dis=distance(resource.DR,resource.UR,boat.DR,boat.UR);
                               if(dis<dismin)
                               {
                                  dismin=dis;
                                  source=resource;
                               }
                            }
                        }
                        HumanAction(fishboats[fishboat].SN, source.SN);
                        fishboat++;
                        return;
                    }
                }



        }
        break;
    }

}
